﻿@using DNA.Mvc.Community.Controllers;
@{
    Layout = "~/Views/Shared/_Dashboard.cshtml";
    HierarchicalNodeProviderBase provider = ViewBag.ForumNodesProvider;
    var context = WebSiteContext.Current;
    
}
@UIHelper.ImageTitle(Html.Global("forums", "forums"), Html.Global("forums", "Forums_Tips"), "~/content/images/icon_forums_48.png")
<table style="width: 100%; border-collapse: collapse;">
    <tr>
        <td colspan="2">
            @{
                Ajax.Dna().Toolbar("_toolbarForForum")
                               .Buttons(buttons =>
                               {
                                   buttons.AddLink(Html.Global("forums", "ForumHome"), Url.Content("~/community/forum")).ImageIcons(Url.Content("~/content/images/icon_home_16.png"));
                                   buttons.AddSpliter();

                                   if (context.IsAuthorized<ForumController>("Create"))
                                   {
                                       buttons.AddButton(Html.Global("forums", "AddSubGroup"), "$.get('" +
                                           Url.Action("Create", "Forum", new { Area = "Community" }) +
                                           "',{id:$('#forumTree').dtree('option','selectedNode').data('key'),isgroup:true},function(htm){$('#dna_forum_viewer').html(htm);});")
                                           .ImageIcons(Url.Content("~/Content/Images/icon_comments_16.png"));


                                       buttons.AddButton(Html.Global("forums", "AddSubForum"), "$.get('" +
                                           Url.Action("Create", "Forum", new { Area = "Community" }) +
                                           "',{id:$('#forumTree').dtree('option','selectedNode').data('key')},function(htm){$('#dna_forum_viewer').html(htm);});")
                                           .ImageIcons(Url.Content("~/Content/Images/comment.png"));
                                  
                                   buttons.AddSpliter(); 
                                   }
                                   if (context.IsAuthorized<ForumController>("Delete")){
                                   buttons.AddButton(Html.Global("RES_DELETE"), Ajax.GeneratejQueryAjaxScripts(new jQueryAjaxOptions()
                                   {
                                       HttpMethod = "POST",
                                       Url = Url.Action("Delete", "Forum", new { Area = "Community" }),
                                       Confirm = Html.Global("forums", "ForumDeleteConfirm"),
                                       Data = new { id = "_selectedKey" },
                                       AutoBindingParameterElement = true,
                                       OnSuccess = "_delNode();"
                                   })).ImageIcons(Url.Content("~/Content/Images/delete.png"));
                                   }
                                   
                                   buttons.AddSpliter();
                                   buttons.AddButton(Html.Global("RES_HELP"), "window.open(\"" + Url.Help(8) + "\");").ImageIcons(Url.Content("~/content/images/icon_help_16.png"));
                               })
                              .Render();
            }
        </td>
    </tr>
    <tr>
        <td style="vertical-align: top; width: 310px;">
            <input id="_selectedKey" type="hidden" value="0" />
            @( Ajax.Dna().TreeView("forumTree")
                            .HtmlAttributes(new { style = "width:300px;height:600px;padding:5px;overflow:auto;margin:0px;" })
                           .Options(opts =>
                           {
                               opts.EnableDropAndDrag = true;
                               opts.OnNodeSelected = "var _key=$(node).data('key');  $('#_selectedKey').val(_key); if (_key==\"0\") $('#dna_forum_viewer').load('" + Url.Action("Welcome", "Forum", new { Area = "Community" }) + "'); else  $.get('" + Url.Action("Edit", "Forum", new { Area = "Community" }) + "',{id:_key},function(htm){$('#dna_forum_viewer').html(htm);});";
                               opts.OnNodeDropped = "$.post('" + Url.Action("Move", "Forum", new { Area = "Community" }) + "', { id: ui.node.data('key'), pos: ui.position,parentID:ui.targetNode.data('key') });";
                           })
                           .Bind<HierarchicalNode>(provider.RootNode, (node =>
                           {
                               if (node.Item != null)
                               {
                                   var forum = (Forum)node.Item;

                                   if (forum.IsGroup)
                                       node.ImageUrl = Url.Content("~/content/images/icon_comments_16.png");
                                   else
                                   {
                                       node.ImageUrl = Url.Content("~/content/images/comment.png");
                                       node.Title += " (" + forum.Threads.Count.ToString() + ")";
                                   }
                               }
                               else
                               {
                                   node.ImageUrl = Url.Content("~/content/images/users_16.png");
                               }
                               node.NavigateUrl = "javascript:void(0);";
                               return true;
                           })).GetHtml())
        </td>
        <td style="vertical-align: top;">
            <div id="dna_forum_viewer">
            @{Html.RenderAction("Welcome", "Forum", new { Area = "Community" }); }
            </div>
        </td>
    </tr>
</table>
<script type="text/javascript">
    function _delNode() {
        var _n = $("#forumTree").dtree("option", "selectedNode");
        if (_n.data("key") == "0") return;
        var _p = _n.parents(".d-treenode");
        if (_p.length) {
            $("#forumTree").dtree("select", _p);
            if (_p.data("key") == "0") $("#dna_forum_viewer").html("");
        }
        _n.remove();
    }
</script>
@{Html.RegisterStartupScript("var _rootNode=$(\"#forumTree\").dtree(\"findNodeElement\",\"data-key\",\"0\"); $(\"#forumTree\").dtree(\"select\",_rootNode);");}
@Html.StartupScripts()
